package com.xhdx;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

public final class ReadFile {
    /**
      * 读取txt文件的内容 *
      *  @param file 想要读取的文件对象
     * @return 返回文件内容
     */
    public static String txt2String(File file){
        StringBuilder result = new StringBuilder();
        try{
            BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
            String line = null;
            while((line = br.readLine())!=null){//使用readLine方法，一次读一行
                if(!(StringUtils.isBlank(line) || line.contains("--") || line.contains("/*"))){
                    result.append(line+System.lineSeparator());
                }
                    
            }
            br.close();    
        }catch(Exception e){
            e.printStackTrace();
        }
        return result.toString();
    }
    /**
     * 读取txt文件的已传参的参数串开头和结尾串的一段内容
     *  @param file 想要读取的文件对象
     *  @param startStr 开头串唯一
     *  @param endStr 结束串，扫描到第一次结束，不传以";"分号结尾
    * @return 返回文件内容
    */
   public static String txt2String(File file, String startStr, String endStr){
       StringBuilder result = new StringBuilder();
       if(StringUtils.isBlank(endStr)){
           endStr = ";";
       }
       try{
           BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
           String line = null;
           boolean flag = false;
           while((line = br.readLine())!=null){//使用readLine方法，一次读一行
               if(line.contains(startStr)){
                   flag = true;
               }
               if(flag && !line.contains("--") && !line.contains("/*")){
                   result.append(line+" ");
               }
               if(flag && line.contains(endStr)){
                   flag = false;
                   break;
               }
           }
           br.close();    
       }catch(Exception e){
           e.printStackTrace();
       }
       return result.toString();
   }
   /**
    * 返回字符串及替换了多少个
    * @param oldS 原来字符串
    * @param replaceS 需要替换的字符串
    * @param newS 需要替换成的字符串
    * @return
    */
   public static Map<String, Object> getMap(String oldS, String replaceS, String newS){
       Map<String, Object> result = new HashMap<String, Object>();
       String newStr = oldS.replaceAll(replaceS, newS);
       result.put("newStr", newStr);
       String[] p = newStr.split(newS);
       result.put("cunt", p == null ?0:p.length-1);
       return result;
   }
   public static Object[] getArr(String dt , int cunt){
       Object[] s = new Object[cunt];
       for(int i=0;i<cunt;i++)
           s[i] = dt;
       return s;
   }
    public static void main(String[] args){
        File file = new File("D:\\work\\文档\\项目\\海口农商\\文档\\需求文档\\日终\\20200710日终\\实验用的.sql");
//        System.out.println(txt2String(file));
        String ss = txt2String(file,"-- （BB1）表名：T_Stat_AcctSum_daily",";");
        System.out.println(ss);
        
        String ss1= txt2String(file,"-- 现金存款的当日","Bussi_otherbank_amount    = (Bussi_otherbank_in_amount)");
        System.out.println(ss1);
        Map<String, Object> p = getMap(ss, "curdate\\(\\)","\\?");
        System.out.println(p.get("newStr")+":"+p.get("cunt"));
        
        Object[] x = getArr("20189" , 2);
        for(int i=0;i<2;i++)
            System.out.println(x[i]);
        Map<String, Object> p1 = getMap(ss1, "Bussi_deposit_count","\\?");
        System.out.println(p1.get("newStr")+":"+p1.get("cunt"));
    }
}
